var express = require('express');
var router = express.Router();
var multiparty = require('multiparty')
var { merchantModel, usersModel, videoModel, merchantTypeModel, couponModel } = require('../module/model')

router.post('/upload', (req, res) => {
    let form = new multiparty.Form()
    form.uploadDir = 'upload'
    form.parse(req, (err, findDate, image) => {

        console.log(image);

        res.send({
            code: 200,
            path: 'http://localhost:3000/yjr/' + image.file[0].path
        })
    })
})

router.post('/merchantAdd', (req, res) => {
    merchantModel.create(req.body)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})

router.get('/videoModel', async (req, res) => {
    
    const video = await videoModel.find()
    let vlength = video.length
    let tlength = video.filter(item => item.thumbsUp > 0).length
    
    res.send({
        code: 200,
        video,
        vlength,
        tlength
    })
})

router.post('/update', async (req, res) => {
    const { _id } = req.body
    console.log(req.body);

    await merchantModel.updateOne({ _id }, req.body)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})

router.get('/merchant', async (req, res) => {
    const merchant = await merchantModel.find()

    res.send({
        code: 200,
        merchant,
        msg: '渲染成功'
    })
})

router.post('/merchantTypeAdd', (req, res) => {
    merchantTypeModel.create(req.body)
    res.send({
        code: 200,
        msg: '类型添加成功'
    })
})

router.post('/couponAdd', (req, res) => {
    couponModel.create(req.body)
    res.send({
        code: 200,
        msg: '套餐券添加成功'
    })
})

router.get('/detail', async (req, res) => {
    const { _id } = req.query
    console.log(_id);

    const detail = await merchantModel.findOne({ _id: _id }).populate('type')

    res.send({
        code: 200,
        detail,
        msg: '渲染成功'
    })
})

router.get('/massage', async (req, res) => {
    const { _id } = req.query
    console.log(_id);

    const massage = await merchantModel.findOne({ _id: _id }).populate('type')

    res.send({
        code: 200,
        massage,
        msg: '渲染成功'
    })
})


module.exports = router;
